package com.huatek.framework.service;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.huatek.base.service.BaseServiceImpl;
import com.huatek.framework.entity.FwDuty;
import com.huatek.framework.entity.FwDutyAction;
import com.huatek.framework.entity.FwSrcAction;
import com.huatek.framework.tag.CommonBean;
import com.huatek.framework.tag.CutPageBean;
import com.huatek.framework.tag.QueryPageBean;

public class RoleServiceImpl extends BaseServiceImpl implements RoleService {

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		super.hibernateTemplate = hibernateTemplate;
	}
	public void saveRole(FwDuty duty) {
				this.saveOrupdate(duty);
	}

	public void assignOperation(Long roleId, Long[] operationIds){
		FwDuty duty = (FwDuty)super.getHibernateTemplate().get(FwDuty.class, roleId);
		duty.getFwDutyActions().clear();
		for(int i=0;i<operationIds.length;i++){
			FwDutyAction dutyAction = new FwDutyAction();
			dutyAction.setFwDuty(duty);
			FwSrcAction fwSrcAction = new FwSrcAction();
			fwSrcAction.setId(operationIds[i]);
			dutyAction.setFwSrcAction(fwSrcAction);
			duty.getFwDutyActions().add(dutyAction);
		}
		super.update(duty);
	}

	@SuppressWarnings("unchecked")
	public List<FwDuty> getAvailableRoles() {
		return hibernateTemplate.find("from FwDuty t where t.status='A' order by t.id asc");
	}

	public FwDuty getRoleById(Long roleId) {
		return (FwDuty)hibernateTemplate.get(FwDuty.class, roleId);
	}

	public CutPageBean getRolePageBean(CutPageBean pageBean,
			List<CommonBean> paramterList) {
		String queryCondition = " 1=1 ";
		String totalRowsHsql = " select count(t) from FwDuty t where"+queryCondition;
		String resultSql = "from FwDuty t where "+queryCondition;
		return QueryPageBean.getQueryPageBean(hibernateTemplate, totalRowsHsql, resultSql,"order by t.id asc", pageBean,paramterList);
	}

}
